package org.mclll.xwangplugin.dengmi.command;

import com.google.common.collect.Lists;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.apache.commons.lang3.tuple.Pair;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mclll.xwangplugin.MclllCustom;
import org.mclll.xwangplugin.dengmi.GameQuestion;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DengMiGameCommand implements TabExecutor {
    private MclllCustom plugin;
    private BukkitTask task;

    public DengMiGameCommand(MclllCustom plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        File file = new File(plugin.getDataFolder(), "dengMi.bin");
        if ("top".equals(args[0])) {
            List<Pair<String, Integer>> list = Lists.newArrayList();
            if (GameQuestion.dengMiTop == null) {
                if (!file.exists()) {
                    sender.sendMessage(Component.text("【").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD)
                            .append(Component.text("新春灯谜").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
                            .append(Component.text("】").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD))
                            .append(Component.text("未找到游戏数据，活动未开启，或已结束 ~").color(NamedTextColor.GREEN).decorate(TextDecoration.BOLD)));
                    return true;
                }
                GameQuestion.recoverData(plugin, null);
            }
            sender.sendMessage(Component.text("----新春灯谜排行榜----").color(NamedTextColor.YELLOW));
            GameQuestion.dengMiTop.forEach((name, score) -> {
                list.add(Pair.of(name, score));
            });
            list.sort(Comparator.comparingInt(Pair::getValue));
            for (int i = 0; i < list.size(); i++) {
                Pair<String, Integer> pair = list.get(list.size() - i - 1);
                if (pair.getKey().equals(sender.getName()) || i < 10) {
                    sender.sendMessage(Component.text(String.format("§l§e%d.%s：§l§2%d", i + 1, pair.getKey(), pair.getValue())));
                }
            }
            return true;
        }
        if (!sender.isOp()) {
            return false;
        }
        switch (args[0]) {
            case "newGame":
                if (this.task != null) {
                    task.cancel();
                    GameQuestion.overGame();
                }
                GameQuestion.reload(sender, plugin);
                task = GameQuestion.start(false);
                return true;
            case "goOn":
                if (task != null) {
                    sender.sendMessage(Component.text("【").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD)
                            .append(Component.text("新春灯谜").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
                            .append(Component.text("】").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD))
                            .append(Component.text("活动已经在进行中了！").color(NamedTextColor.RED).decorate(TextDecoration.BOLD)));
                    return true;
                } else if (!file.exists()) {
                    sender.sendMessage(Component.text("【").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD)
                            .append(Component.text("新春灯谜").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
                            .append(Component.text("】").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD))
                            .append(Component.text("未找到游戏数据，活动未开启，或已结束 ~").color(NamedTextColor.RED).decorate(TextDecoration.BOLD)));
                    return true;
                }
                GameQuestion.recoverData(plugin, sender);
                task = GameQuestion.start(true);
                return true;
            case "overGame":
                if (this.task != null) {
                    task.cancel();
                }
                GameQuestion.overGame();
                return true;
            case "stop":
                if (this.task == null) {
                    sender.sendMessage(Component.text("【").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD)
                            .append(Component.text("新春灯谜").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
                            .append(Component.text("】").color(TextColor.color(182, 182, 223)).decorate(TextDecoration.BOLD))
                            .append(Component.text("活动未开启，或已结束 ~").color(NamedTextColor.RED).decorate(TextDecoration.BOLD)));
                    return true;
                }
                task.cancel();
                GameQuestion.stop();
            default:
        }
        return false;
    }

    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (!sender.isOp()) {
            return Collections.singletonList("top");
        }
        return Lists.newArrayList("newGame", "overGame", "top", "goOn", "stop");
    }
}
